﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ECI.CommonLibrary;
using System.Data;
using System.Web;
using ECI.Entities;
using ECI.Controls;
using ECI.CoreLibrary;
using ECI.WebLib.Data;

namespace ECI.DynamicData.ElementBuilder
{

    /// <summary>
    /// 创建文本框
    /// </summary>
    public class TInput : IElement
    {
        private TextBox txtBox = null;
        public override void Create(ElementParameter param)
        {
            this.Init(param);
            this.CreateContainer();

            txtBox = new TextBox();

            if (param.Field.DictionaryType == "INPUT_INT" || param.Field.DictionaryType == "INPUT_NUMBER")
            {
                txtBox = new ECIDataInput();
                ((ECIDataInput)txtBox).IsNum = param.Field.DictionaryType == "INPUT_INT";
                ((ECIDataInput)txtBox).IsFloat = param.Field.DictionaryType == "INPUT_NUMBER";

                if ((txtBox as ECIDataInput).IsNum)
                {
                    txtBox.MaxLength = param.Field.DictionaryTypeConfig.ToIntIgnoreError();
                    if (txtBox.MaxLength == 0)
                    {
                        txtBox.MaxLength = 11;
                    }
                }
            }
            else
            {
                txtBox.MaxLength = Field.FieldLength.ToIntIgnoreError();

            }

            txtBox.Attributes.Add("sql", param.Field.SqlAttribute);
            txtBox.Attributes.Add("mainalias", param.Field.MainAlias);
            txtBox.Attributes.Add("maxlength", Field.FieldLength.ToString());

            if (param.Field.DictionaryTypeConfig.ToUpper() == "PASSWORD")
            {
                txtBox.TextMode = TextBoxMode.Password;
            }
            //2010-06-29 增加控件宽度属性控制 控件的显示宽度
            if (param.type == "Edit")
            {
                if (param.Field.ControlWidth.Length > 0)
                {
                    txtBox.Style["width"] = param.Field.ControlWidth;
                }
                else
                {
                    txtBox.Style["width"] = "85%";//占整个单元格的85%
                }
            }
            else
            {
                txtBox.Style["width"] = "85%";//占整个单元格的85%
            }

            #region 文本框输入内容是否忽略大小写
            if (param.Field.SqlAttribute.ToLower().Contains("ignore"))
            {
                txtBox.Attributes.CssStyle.Add("text-transform", "uppercase");
            }
            #endregion

            #region 相关控制
            //if (param.type == "Edit") DynamicQuery.EditLockControl(txtBox, param.DataRow, param.Field, param.EditMode);//会根据是否新增 编辑状态动态显示编辑模式
            //if (param.type == "Edit") DynamicQuery.EditDefaultControl(txtBox, param.DataRow, param.Field);
            //if (param.type == "Edit" && param.Field.SystemData.ToMyString() != "") DynamicQuery.LockControl(txtBox);//系统字段锁定
            //if (param.type == "Edit") DynamicQuery.CascadeControl(txtBox, param.DataRow, param.Field);
            //if (param.type == "Edit") DynamicQuery.EditSystemFieldShowValue(txtBox, param.DataRow, param.Field);//系统字段-是否在新增时就显示
            #endregion

            #region 控件命名
            string contolId = "txt" + param.Field.Name; //控件的名称 统一 txt + 字段名
            if (param.EditMode == "BatchUpdate") contolId = "txtBatch" + param.Field.Name;//批量修改时 控件名称 txtBatch + 字段名
            txtBox.ID = contolId;
            #endregion

            #region 控件赋值:在处于编辑的状态下
            if (param.DataRow != null)
            {
                string value = GetDataValueForEditPage();
                txtBox.Text = value;
            }
            else
            {
                if (this.Field.DataSource.Length > 0)
                {
                    txtBox.Text = GetDataValueForEditPage();
                }
            }
            #endregion


            //在编辑的情况下：如果是系统字段，根据是否显示
            if (param.type == "Edit" && DynamicQuery.EditHideForSystemField(param.Field))
            {
                param.HideCell.Controls.Add(txtBox);
            }
            else if (param.Field.Hidden == "Y")
            {
                param.HideCell.Controls.Add(txtBox);
            }
            else
            {
                if (Field.AfterElement != "")
                {
                    TableCell cell = this.GetControlCell(param.TableRow.Parent as Table, Field.AfterElement.Trim());
                    if (cell == null)
                        throw new Exception(Field.MainAlias + ": 没找到上级元素" + Field.AfterElement);
                    Label lab = new Label();
                    lab.Text = Field.Udf2;
                    cell.Controls.Add(lab);
                    cell.Controls.Add(txtBox);
                }
                else
                {
                    MyTableCell.Controls.Add(txtBox);//新增文本框控件
                    param.TableRow.Cells.Add(MyTableCell);
                }
            }


            #region 复制新增时 是否清除控件内容
            if (param.Field.CopyRemoveData.ToMyString() == "1" && param.EditMode.ToLower() == "copy")
            {
                if (this.Field.DataSource.Length > 0)
                    txtBox.Text = GetDataValueForEditPage();
                else
                    txtBox.Text = Field.DefaultValue;
            }
            #endregion



        }

        #region IElement 成员


        public override void Locked()
        {
            DataRow dataRow = param.DataRow;
            string editMode = param.EditMode;

            if (Field.EditLockMode == "LOCKFULL") LockControl();
            else if (dataRow != null && editMode.ToLower() != "copy" && Field.EditLockMode == "LOCKEDIT") LockControl();
            else if (dataRow == null && Field.EditLockMode == "LOCKADD") LockControl();

        }

        public override void Default()
        {
            DataRow dataRow = param.DataRow;
            if (Field.DefaultValue != "" && dataRow == null)
            {
                txtBox.Text = this.GetDefaultDataForControl();
            }
        }

        public override void QueryDefault()
        {
            if (Field.AutoCodeType != "")
            {
                txtBox.Attributes.Add("autocomplete", "off");
                txtBox.Attributes.Add("onkeyup", "AutoTip(this,'" + Field.Guid + "')");
            }
            if (Field.DefaultValueQuery != "" && param.type.ToLower() == "query")
            {
                txtBox.Text = this.GetQueryDefaultDataForControl();
            }
            if (Field.Udf6 == "Y")
                LockControl();
        }

        public override void SystemField()
        {
            DataRow dataRow = param.DataRow;
            string editMode = param.EditMode;
            txtBox.ReadOnly = true;
            txtBox.BackColor = System.Drawing.Color.Gainsboro;
            if (txtBox.CssClass == "Wdate") txtBox.Attributes.Remove("onclick");
            if (txtBox is ECIChoose) ((ECIChoose)txtBox).IsGeneric = true;


            if (Field.SystemData.ToMyString() != "" && Field.SystemDataTrigger.ToMyString() == "INSERT" && Field.SystemDataShow.ToMyString() == "1" && dataRow == null)
            {
                txtBox.Text = GetSystemDataValue();
            }
        }

        public override void Cascade()
        {
            DataRow dataRow = param.DataRow;
            if (dataRow == null)
            {
                string value = PageHelper.Request(Field.Name);
                if (value != "")
                {
                    txtBox.Text = value;
                }
            }
        }

        #endregion

        private void LockControl()
        {
            txtBox.ReadOnly = true;
            txtBox.BackColor = System.Drawing.Color.Gainsboro;
            if (txtBox.CssClass == "Wdate") txtBox.Attributes.Remove("onclick");
            if (txtBox is ECIChoose) ((ECIChoose)txtBox).IsGeneric = true;
        }


        public override string GetValue()
        {

            //系统字段 是不可以批量修改的 否则这里面逻辑有问题   

            string value = "";

            if (param.editMode == DynamicEditMode.BatchUpdate)
            {
                CheckBox chk = param.Table.FindControl("chk" + Field.Name) as CheckBox;
                if (chk.Checked)
                {
                    TextBox txtBox = param.Table.FindControl("txtBatch" + param.Field.Name) as TextBox;
                    if (param.Field.SqlAttribute.ToLower().Contains("ignore"))
                    {
                        txtBox.Text = txtBox.Text.Trim().ToUpper();
                    }

                    value = txtBox.Text;

                    return DynamicQuery.GetDynamicSQL(param.Field, value, DynamicEditMode.Update);

                }
                else
                {
                    return "";
                }

            }
            else
            {
                if (param.Field.SystemData.ToMyString() == "")
                {
                    TextBox txtBox = param.Table.FindControl("txt" + param.Field.Name) as TextBox;

                    if (param.editMode == DynamicEditMode.Insert && param.Field.PrimaryCreator.Trim().Length > 0)
                    {
                        ECI.BaseForm.IRecord record = PageHelper.CurrentPage as ECI.BaseForm.IRecord;
                        ECI.BaseForm.UITransData UITransData = record.GetUITransData();

                        ECI.BaseForm.UITransModel UIModel = new ECI.BaseForm.UITransModel();
                        UIModel.ClientId = "txt" + param.Field.Name;
                        UIModel.OldValue = "";
                        UITransData.ListUITransElement.Add(UIModel);

                        object ts = record.GetTransaction();
                        txtBox.Text = DynamicNo.GetNo(param.Field.PrimaryCreator, ts);
                    }

                    if (param.Field.SqlAttribute.ToLower().Contains("ignore"))
                    {
                        txtBox.Text = txtBox.Text.Trim().ToUpper();
                    }

                    value = txtBox.Text;
                }
                else
                {
                    value = this.GetSystemDataValue();
                }

                return DynamicQuery.GetDynamicSQL(param.Field, value, EditMode);
            }

        }


        public override void DataKeep()
        {
            if (PageHelper.CurrentPage.IsPostBack)
            {
                if (param.Field.DataKeep == "Y")
                {
                    this.txtBox.Text = PageHelper.Request("txt" + param.Field.Name);
                }
            }
        }

        public override void ClientEventRegister()
        {

        }

        public override string GetRealValue()
        {
            throw new NotImplementedException();
        }
    }
}
